package com.heima.search.controller.v1;


import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.dtos.UserSearchDto;
import com.heima.model.search.vos.SearchArticleVo;
import com.heima.model.user.pojos.ApUser;
import com.heima.search.service.HistoryService;
import com.heima.search.service.SearchService;
import com.heima.utils.thread.AppThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/v1/article/search")
public class ArticleController {

    @Autowired
    private SearchService searchService;

    @Autowired
    private HistoryService historyService;
    /**
     * app端文章查询
     * @param dto
     * @return
     */
    @PostMapping("/search")
    public ResponseResult search(@RequestBody UserSearchDto dto){

        ApUser user = AppThreadLocalUtil.getUser();
        if(user == null){
            throw new CustomException(AppHttpCodeEnum.NEED_LOGIN);
        }

        historyService.save(user.getId(),dto.getSearchWords());

       List<SearchArticleVo> list =  searchService.search(dto);
       return ResponseResult.okResult(list);
    }
}
